/*
Project: 		The Impact of Short-Term Employment for Low-Income Youth: Experimental 	
				Evidence from the Philippines
Authors: 		Emily A. Beam and Stella Quimbo

***********************************
Path: 			code/02_analysis
File name: 		table_02.do 
***********************************

Purpose: 		Generates first-stage and summer employment results

Inputs: 		spes_data.dta
Outputs: 		table2_iv_`mes'.tex						*Table 2 = table2_iv_spes_2016.tex
														*Table X = table2_iv_tr.tex

Log: 			results/03_output/log_table_02.log		

Last updated: 	16 November 2021
*/ 


cap log close 

log using "$output/log_table_02.log",replace

estimates clear 
use "$usedata_analysis/spes_data.dta",clear 
	
	keep if endline == 1

local n: word count $cov1
	assert `n' == 18 

** First stage  predicting spes_2016 

local r "replace" 
areg spes_2016 treatment $cov1, r absorb(scell)
estimates store spes_2016
sum spes_2016 if treatment == 0 
local dmean = `r(mean)'
estadd scalar dmean = `r(mean)'

testparm treatment 
estadd scalar Fstat = `r(F)'

local Fstat = `r(F)'
** First stage  predicting tr 

local r "replace" 
areg tr treatment $cov1, r absorb(scell)
estimates store tr
sum tr if treatment == 0 
local dmean = `r(mean)'
estadd scalar dmean = `r(mean)'

testparm treatment 
estadd scalar Fstat = `r(F)'

local Fstat = `r(F)'


* Set locals

local depvar _eeo_worksummer_any_spes    _eeo_worksummer_totearn _eeo_worksummer_earn_all

local labels " "Summer work, any"  "Summer, total non-SPES earnings" "Total summer earnings" "
local i = 1


local r replace

local i = 1


	foreach var in `depvar' {
	
		areg `var' treatment $cov1 ,r absorb(scel)
		estimates store `var'
		summ `var' if treatment==0 
		estadd scalar dmean = `r(mean)'
		

foreach mes in  spes_2016 tr{
		xi:  ivregress 2sls  `var' (`mes' = treatment ) $cov1  i.scel if treatment!=. ,  robust
		
		estimates store iv`mes'`i'
		summ `var' if treatment==0 
		estadd scalar dmean = `r(mean)'


		
}
local i = `i' + 1
}


#delimit ; 
********** Table 2 - work type *************;

local fnt2a= "\multicolumn{3}{p{0.8\textwidth}}{\footnotesize{Notes: All endline respondents included. All specifications include controls listed in Table \ref{tbal} along with stratification-cell fixed effects. Column 4 sums self-reported earnings with administrative earnings from SPES records. Consequently, 65 respondents who did not report enrolling in SPES are recorded as having non-zero SPES earnings. \textit{*** p$<$0.01, ** p$<$0.05, * p$<$0.10} }}\\". ;

local fnt2= "\multicolumn{6}{p{\textwidth}}{\footnotesize{Notes: All endline respondents included. All specifications include controls listed in Table \ref{tbal} along with stratification-cell fixed effects. Column 4 sums self-reported earnings with administrative earnings from SPES records. Consequently, 65 respondents who did not report enrolling in SPES are recorded as having non-zero SPES earnings. \textit{*** p$<$0.01, ** p$<$0.05, * p$<$0.10} }}\\". ;


cd "$tables_analysis";
	// Output Tables ;


foreach mes in  spes_2016{;
	
	esttab  spes_2016  tr iv`mes'1		iv`mes'2		iv`mes'3	  
	using table_02.tex, replace cells("b(fmt(3) star)" "se(par([ ]))") 
	keep(treatment `mes') label  varwidth(16) stat(N dmean Fstat, fmt(%9.0f %9.3f %9.0f ) 
	labels( "Observations" "Mean, control group" "F-test statistic"))modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) varlabels(_cons Constant) 
	mlabels("Self-reported" "Admin" 	 
				"Any summer work" 
				 "Total earnings" 
				 "Total earnings, inc. SPES", 
			span prefix(\multicolumn{@span}{x{0.14\textwidth}}{) suffix(})) 	
	collabels(none) 
	mgroups("First stage" "Summer employment and earnings", pattern( 1 0 1  0   0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{@span}))
	prehead( "\begin{tabular}{lx{0.12\textwidth}x{0.12\textwidth}x{0.12\textwidth}x{0.12\textwidth}x{0.14\textwidth}}" "\toprule") 
	posthead(\hline) prefoot() 
			postfoot("\bottomrule" "`fnt2'" "\end{tabular}");

};


log close;
